<div id="content-header">
<h1>Edit content</h1>
</div>
<div id="breadcrumb">
<a href="<?php echo $this->Html->url('/admin/')?>" title="Go to Home" class="tip-bottom"><i class="icon-home"></i> Home</a>
<a href="#">Edit</a>
<a href="#" class="current"><?php echo $datas['Node']['title']?></a>
</div>
<div class="container-fluid">

<div class="row-fluid">
    <div class="span12">
 <?php
    echo $this->Form->create('Node', array('id' => 'paginate','class'=>'form-horizontal', 'action' => 'edit', 'url' => $this->request->here(false), 'type' => 'file',
        'inputDefaults' => array('label' => false, 'format' => array('before', 'label', 'between', 'input', 'after', 'error'))));

    echo $this->Html->script('ckeditor/ckeditor');
    echo $this->element('toolbar');

    ?>    
                        
        <div class="widget-box">

            <?php            
            $fieldsets['term_id'] = $this->Form->input('term_id', array(            
            'type' => 'select',
            'options' => $listTerms,
            'value' => $nodeTerm['NodeTerm']['term_id'],
            'empty' => '-- None --',
            'label' => array('class' => 'control-label'),
            'between' => '<div class="controls">',
            'div' => array('class' => 'control-group'),
            'after' => '</div>',
            'escape' => false));  
                     
            $fieldsets['user_id'] = $this->Form->input('user_id', array(
            'label' => array('class' => 'control-label'),
            'between' => '<div class="controls">',
            'div' => array('class' => 'control-group'),
            'after' => '</div>',
            'type' => 'hidden'));
            ?>
            <?php 
                $fieldsets['title'] = $this->Form->input('title', array(
                'label' => array('text'=> __d('system','Title'),'class' => 'control-label'),
                'between' => '<div class="controls">',
                'div' => array('class' => 'control-group'),
                'after' => '</div>',
                'type' => 'text'));                               
            $fieldsets['node_term_id'] = $this->Form->input('node_term_id', array(
                'label' => array('class' => 'control-label'),
                'between' => '<div class="controls">',
                'div' => array('class' => 'control-group'),
                'after' => '</div>',
                'value' => $nodeTerm['NodeTerm']['id'],
                'type' => 'hidden'));              
            $fieldsets['is_published'] = $this->Form->input('is_published', array(
                    'label' => array('text'=>__d('system','Show/Hide'),'class' => 'control-label'),
                    'between' => '<div class="controls">',
                    'div' => array('class' => 'control-group'),
                    'after' => '</div>',
                    'type' => 'checkbox'));  
                     
                                                                      
            ?>
<?php
    $layout = array(
        array('node_term_id', 'user_id'),
        array('title','term_id'),        
        array('is_published'),
    );
    foreach ($fields as $field) {
            $type = 'text';
            if ($field['Field']['field_type'] == 'img' || $field['Field']['field_type'] == 'file') {
                $type = 'file';
            }
             if($field['Field']['field_type'] == 'textarea'){
                $type= 'textarea';
            }
            else if($field['Field']['field_type'] == 'checkbox'){
                $type= 'checkbox';
            }
            $chosseckeditor = null;
            
          switch ($field['Field']['use_wyswyg']) {
            case null:
                $chosseckeditor =null;
                break;
            case 0:
                $chosseckeditor =null;
                break;                
            case 1:
            $chosseckeditor = 'ckeditor';
                break;
        } 
        $fieldsets['field_' . $field['Field']['name']] = $this->Form->input('field_' . $field['Field']['name'], array(
            'label' => array('text'=>$field['Field']['label'],'class' => 'control-label'),
            'between' => '<div class="controls">',
            'div' => array('class' => 'control-group'),
            'after' => '</div>',
            'class'=> $chosseckeditor,
            'type' => $type));
        $layout[] = array('field_' . $field['Field']['name']);
    }
            
            $layout = array(
                array(
                    'Basic setting',
                    $layout,
                )
            );
            echo $this->Layout->form($layout, $fieldsets);
    ?>
        <?php if(isset($images) && !empty($images)){?>
            <div class="control-group add-more">
                <label class="control-label" for="NodeFieldImage">Image</label>
                    <div class="controls">
                        <?php echo $this->Form->input('ProImage', array(
                            'class'=>'file-upload',
                            'label' => false,
                            'type'  => 'file', 
                            'name' => 'data[ProImage][]'));?>
                    
                    </div>
                                                                           
           </div>    
  <div id="cloneImage"></div>
        <a style="padding-left: 10px; clear: both; display: block;" href="javascript:add_more();"><?php echo __d("system", "Add more image")?></a>                                                   
        <?php }?>   
             <?php if(!empty($galleries)){?>
    <div class="widget-box">
        <div class="widget-title">
            <span class="icon">
                <i class="icon-picture"></i>                                    
            </span>
            <h5>List of Images</h5>
        </div>
        <div class="widget-content">
            <div class="row-fluid row-fluid-two">
                <ul class="thumbnails">
                            <?php if(!empty($galleries)){?>
                    <?php foreach($galleries as $key_image=>$image){
          
                 
                    ?>
                    <li class="span3">
                        <div class="thumbnail">
                        <?php echo $this->Html->image('/img/'.$image, array('width'=>'287', 'height'=>'137', 'alt'=> 'Image')) ?>
                            <div class="wd-delete-edit">
                                  <a href="<?php echo $this->Html->url(array('plugin'=>'node','controller' => 'node', 'action' => 'edit_image',$typeId, $datas['Node']['id'],$key_image));?>"><i class="icon-pencil icon-white">&nbsp;</i> Edit</a>
                                <a id="image_del_<?php echo $key_image; ?>" href="javascript:void(0);" onclick="delete_image('<?php echo $key_image?>');"><i class="icon-remove icon-white">&nbsp;</i> Delete</a>
                            </div>
                        </div>
                    </li>
                <?php }?> 
                <?php } ?> 
                </ul>
            </div>
        </div>
    </div>        
    <?php }?>
        </div>
    <?php
    echo $this->Form->hidden('node_type_id');
    echo $this->Form->hidden('Paginate.task', array('id' => 'task', 'value' => ''));
    echo $this->Form->end();
    ?>                                    
    </div>    
</div>
<!--
<div class="row-fluid">
    <div class="span12">
        <div class="widget-box">
            <div class="widget-title">
                <span class="icon">
                    <i class="icon-align-justify"></i>                                    
                </span>
                <h5>SEO OPTIONS</h5>
            </div>
            <div class="widget-content nopadding">
                <form action="#" method="get" class="form-horizontal" />
                    <div class="control-group">
                        <label class="control-label">Title</label>
                        <div class="controls">
                            <input type="text" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">Keywords</label>
                        <div class="controls">
                            <input type="text" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">Description</label>
                        <div class="controls">
                            <textarea></textarea>
                        </div>
                    </div>
                </form>
            </div>
        </div>    
    </div>
</div>
-->

<script type="text/javascript">
jQuery(document).ready(function($){
    $('<span>Size: 1600x850</span>').insertAfter("#NodeFieldBackgroundImage");
    $('<span>Enter a valid number: (int)</span>').insertAfter("#NodeFieldBackgroundPiority");
    $('#NodeFieldNewPostDate').datetimepicker({
        dateFormat: 'yy-mm-dd',
        timeFormat: 'hh:mm'
    });
    $("input").prop('disabled', true);
    $("select").prop('disabled', false);
    $("textarea").prop('disabled', true);    
});
function add_more(){
    var element= jQuery("#cloneImage").prev();
    jQuery(element).clone().insertBefore("#cloneImage");
    var $this = Green.Elfinder;
    jQuery('form input.file-upload').prop('readonly' , true).click(function(){
                $this.show(jQuery(this));
            });
}
function delete_image(id){
    if (confirm("Are you sure you want to delete?")) {
        var href= "<?php echo '/node/node/delete_image/'?>"+id;
        jQuery.ajax({
          url: href,
          type: "GET",
          success: function(data) {
            jQuery('#image_'+id).remove();
            jQuery('#image_del_'+id).remove();
            window.location.reload();
            //alert('Load was performed.');
          }
        });
    }
}
</script>